#!/bin/bash

# 项目名称
PROJECT_NAME="xinhou-rocket-admin"
# 构建输出目录
OUTPUT_DIR="target/release"
# 部署服务器地址和路径（根据实际情况修改）
REMOTE_SERVER="user@remote-server.com"
REMOTE_DIR="/path/to/deploy"

# 显示帮助信息
function usage() {
    echo "Usage: $0 {build|rebuild|clean|upload|deploy}"
    exit 1
}

# 构建项目
function build() {
    echo "Building the project..."
    cargo build --release
    if [ $? -ne 0 ]; then
        echo "Build failed!"
        exit 1
    fi
    echo "Build completed successfully."
}

# 重新构建项目
function rebuild() {
    echo "Rebuilding the project..."
    cargo clean
    build
}

# 清理构建文件
function clean() {
    echo "Cleaning the project..."
    cargo clean
    if [ $? -ne 0 ]; then
        echo "Clean failed!"
        exit 1
    fi
    echo "Clean completed successfully."
}

# 上传构建文件到远程服务器
function upload() {
    echo "Uploading the project to remote server..."
    scp "$OUTPUT_DIR/$PROJECT_NAME" "$REMOTE_SERVER:$REMOTE_DIR"
    if [ $? -ne 0 ]; then
        echo "Upload failed!"
        exit 1
    fi
    echo "Upload completed successfully."
}

# 部署项目到远程服务器
function deploy() {
    upload
    echo "Deploying the project on remote server..."
    ssh "$REMOTE_SERVER" "cd $REMOTE_DIR && ./deploy.sh"
    if [ $? -ne 0 ]; then
        echo "Deploy failed!"
        exit 1
    fi
    echo "Deploy completed successfully."
}

# 检查输入参数
if [ $# -ne 1 ]; then
    usage
fi

# 根据输入参数执行相应的函数
case "$1" in
    build)
        build
        ;;
    rebuild)
        rebuild
        ;;
    clean)
        clean
        ;;
    upload)
        upload
        ;;
    deploy)
        deploy
        ;;
    *)
        usage
        ;;
esac
